/**
 * @file Describe the file
 * @author author-name: lihao 逻辑开发
 *         author-name2：lizhiguo(18792718848@163.com)代码优化
 */
(function ($, window){
		var AddDevice_1 = function(options) {
		this.options = $.extend(true, {
			  id: '__MUI_AddDevice_2',
		    }, options || {});
		    this.init();
		    this.initEvent();
	 };
	 var proto = AddDevice_1.prototype;
	    proto.init = function() {
          this.state = {
              secHospital: '',
              secDep: '',
              secPeople: '',
              secDepId: '',
              secPeopleId: ''
          }
	};
	proto.initEvent = function() {
        //声明对应的数组
		  var self = this;
		  $('.DeviceOwnership').on('tap', '#hospitalSelect', function() {
          self.judgePop(0, "#hospPopover");
       });

      $('.DeviceOwnership').on('tap', '#department', function() {
          self.judgePop(1, "#depPopover");
       });

      $('.DeviceOwnership').on('tap', '#principal', function() {
          self.judgePop(2, "#peoPopover");
       });

      $('.DeviceOwnership').on('tap', '#nextStep', function() {
          self.onPress();
       });

      mui('body').on('tap', '.mui-popover', function(e) {
      
      //当用户点击popover后隐藏popover
          mui('.mui-popover').popover('hide');
      });  

		  document.getElementById("hosUl").addEventListener('selected', function(e){
          //获取被选中的医院元素
          self.state.secHospital = e.detail.el.innerText;
          //替换原先标识上的文字
          document.getElementById("hospital").innerText = self.state.secHospital;

          //清除所有子节点
          var depUlList = document.getElementById("depUl");
          for (var i = 0;i < depUlList.childNodes.length; i++){
            depUlList.removeChild(depUlList.childNodes[0]);
          }

          document.querySelector('.mui-btn').style.backgroundColor = "rgba(73,138,247,0.3)";
          //当医院被选好之后，拉取数据，生成第二个select框
          self.getDataByType(1,
            'https://bird.ioliu.cn/v1?url=http://app.xiaoweicanting.com/hospital.php?m=App&c=equipment&a=getNewEquipmentHallList&hospitalid=10&uid=3&token=dfaf');
      
      });

      document.getElementById("depUl").addEventListener('selected', function(e){
          if(!self.state.secHospital){  return; }
          //获取被选中的科室元素
          self.state.secDep = e.detail.el.innerText;
          self.state.secDepId = e.detail.el.id;
          
          document.getElementById("dep").innerText = self.state.secDep;
      
          //清除所有子节点
          var peoUlList = document.getElementById("peoUl");
          for (var j = peoUlList.childNodes.length; j > 0; j--){
            peoUlList.removeChild(peoUlList.childNodes[0]);
          } 
          document.getElementById("people").innerText = "请选择负责人";
      
          document.querySelector('.mui-btn').style.backgroundColor = "rgba(73,138,247,0.3)";
          //当医院被选好之后，拉取数据，生成第三个select框
          self.getDataByType(2,
            'https://bird.ioliu.cn/v1?url=http://app.xiaoweicanting.com/hospital.php?m=App&c=equipment&a=getNewEquipmentHallDocterList',
              {
                hospitalid: 10,
                departmentid: e.detail.el.id,uid: 3,
                token: 'dfaf'}); 
      });

      document.getElementById("peoUl").addEventListener('selected',function(e){
          if (!self.state.secDep){ return;}
          //获取被选中的负责人元素
          self.state.secPeople = e.detail.el.innerText;
          self.state.secPeopleId = e.detail.el.id;
          document.getElementById("people").innerText = self.state.secPeople;
          
          //在此处将button的样式替换为可点击的
          if (self.state.secHospital && self.state.secDep && self.state.secPeople){
               //在以上上颚参数都被赋值的情况下，将button的颜色改变为可点击的
              $("#nextStep")[0].style.backgroundColor = "#498af7";
              $("#nextStep")[0].removeAttribute("disabled");
          }
      
      });

	};

	proto.judgePop = function(type, myPopOver) {
      switch(type){
          case 0:
              $(myPopOver).popover('show');
              break;
          case 1:
              if (this.state.secHospital){
                  $(myPopOver).popover('show');
              }
              break;
          case 2:
              if (this.state.secDep){
                  $(myPopOver).popover('show');
              } 
              break;
          default:
              break;
          }
	}

  proto.getDataByType = function(type, url) {        
          //返回通过myDate请求的数据生成对应的列表
      mui.get(url, function (dataObject){
            //在type为1即拉取科室数据
      if (type === 1){
              //获取对应的depui对象
          var myDepUl = document.getElementById("depUl");
                  //获取对应的数据
          var depList = dataObject.data;
                  //声明需要的ul和a和对应文字标签
          var myLi,myA,textNode;
          for (var i = 0;i < depList.length;i++){
                    //创建li对象
              myLi = document.createElement("li");
              myLi.className = 'mui-table-view-cell';
              myLi.id = depList[i].hid;
              //创建a对象
              myA = document.createElement("a");
              myA.className = 'mui-navigate-right';
              //创建文字数据
              textNode = document.createTextNode(depList[i].hallname);
              myA.appendChild(textNode);
              myLi.appendChild(myA);
              myDepUl.appendChild(myLi);                  
          }
      }
       else {     //在type为2即拉取负责人数据            
              //获取对应的depui对象
          var myPeoUl = document.getElementById("peoUl");
          //获取对应的数据
          var peoList = dataObject.data;
          //声明需要的ul和a和对应文字标签
          var myLi,myA,textNode;
          for (var i = 0;i < peoList.length;i++){
              //创建li对象
              myLi = document.createElement("li");
              myLi.className = 'mui-table-view-cell';
              myLi.id = peoList[i].eid;
              //创建a对象
              myA = document.createElement("a");
              myA.className = 'mui-navigate-right';
              //创建文字数据
              textNode = document.createTextNode(peoList[i].username);
              myA.appendChild(textNode);
              myLi.appendChild(myA);
              myPeoUl.appendChild(myLi);              
              }        
            }     
        }, 'json'
      ); 
  }
        //声明一个方法，当点击下一步时，打开新的页面并传值
  proto.onPress = function() {
      var self = this;
      if(self.state.secHospital && self.state.secDep && self.state.secPeople){
          mui.openWindow({
              url: 'AddDevice_2.html',
              id: 'AddDevice_2.html',
              extras: {
            
                  Hospital: self.state.secHospital,
                  HospitalId: 10,
                  Dep: self.state.secDep,
                  DepId: self.state.secDepId,
                  People: self.state.secPeople,
                  PeopleId: self.state.secPeopleId
              },
              styles: {
                  scrollIndicator: 'none'
              } 
          });
      }
      else {
          mui.toast('请在填写完毕后点击下一步');
          return ;
      }
  }
 
var AddDeviceApi = null;
$.AddDevice = function(options) {
    if (!AddDeviceApi) {
    	AddDeviceApi = new AddDevice_1(options);
    }

    return AddDeviceApi;
};
	})(mui, window)
